package com.blue.controller.system;

import com.blue.common.entity.ResponseResult;
import com.blue.common.utils.RedisUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 缓存信息
 */
@RestController
@RequestMapping("/system/redis")
@Tag(name = "缓存信息")
public class RedisController {

    @Autowired
    private RedisUtils redisUtils;

    @GetMapping("/queryKeys")
    @Operation(summary = "通过键组查询键")
    public ResponseResult<?> queryKeys(@RequestParam("keysGroup") String keysGroup) {
        List<String> keys = redisUtils.queryKeys(keysGroup);
        List<Map<String, String>> maps = new ArrayList<>();
        keys.forEach(item -> {
            Map<String, String> map = new HashMap<>();
            map.put("key", item);
            maps.add(map);
        });
        return ResponseResult.okResult(maps);
    }

    @GetMapping("/queryValue")
    @Operation(summary = "通键查询值")
    public ResponseResult<?> queryValue(@RequestParam("key") String key) {
        Map<String, Object> value = redisUtils.queryValue(key);
        return ResponseResult.okResult(value);
    }

    @GetMapping("/deleteByKey")
    @Operation(summary = "通过key删除键值")
    public ResponseResult<?> deleteByKey(@RequestParam("key") String key) {
        redisUtils.removeValue(key);
        return ResponseResult.okResult();
    }

    @GetMapping("/deleteByKeysGroup")
    @Operation(summary = "通过KeysGroup删除键值")
    public ResponseResult<?> deleteByGroup(@RequestParam("keysGroup") String keysGroup) {
        redisUtils.removeKeys(keysGroup);
        return ResponseResult.okResult();
    }
}
